﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Ajax.Utilities;
using PTop.Application.Questionnaire;
using PTop.Domain.Entity.Questionnaire;
using System.Web.Mvc;
using PTop.Front.Areas.Qn.Models;
using PTop.Front.Controllers;

namespace PTop.Front.Areas.Qn.Controllers
{
    public class QnPaperController : PTopControllerBase
    {
        [HttpGet]
        public ActionResult Paper(string id)
        {
            SetOpenIdCookie();
            var paper = new QnPaperApp().GetForm(id);
            if (paper == null || paper.F_State != 1 || paper.F_ValidTime < DateTime.Now)
            {
                return View("NoPaper");
            }
            var qnPaperModel = new QnPaperModel
            {
                QnPaper = paper
            };
            var questions = new QnQuestionApp().GetListByPaper(paper.F_Id);
            qnPaperModel.QnQuestions = questions.ToArray();
            var answers = new QnAnswerApp().GetListByUser(OpenId, id);
            qnPaperModel.QnAnswers = answers.ToArray();
            return View(qnPaperModel);
        }

        /// <summary>
        /// 判断是否已经答过此问卷调查
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="paperId"></param>
        /// <returns></returns>
        public ActionResult HasSubmit(string openId, string paperId)
        {
            var answers = new QnAnswerApp().GetListByUser(OpenId, paperId);
            if (answers.Any())
            {
                return Success("SUCCESS", true);
            }
            return Success("SUCCESS", false);
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult SubmitForm(string openId, string paperId, string answers)
        {
            var paper = new QnPaperApp().GetForm(paperId);
            if (paper == null || paper.F_State != 1 || paper.F_ValidTime < DateTime.Now)
            {
                return Error("调查问卷不存在或已经过期！");
            }
            //var wechatUser = new UserInfoApp().GetFormByOpenId(openId);
            //if (wechatUser == null)
            //{
            //    return Error("没有您的账号，请关注微信公众号" + Configs.GetValue("WeChatSubscription"));
            //}
            var answerApp = new QnAnswerApp();
            var ases = answerApp.GetListByUser(openId, paperId);
            if (ases.Any())
            {
                return Error("您已经提交过次调查问卷，请勿重复提交！");
            }
            var questions = new QnQuestionApp().GetListByPaper(paperId);
            var answerList = new List<QnAnswerEntity>();
            var qustAnswerStrList = answers.Split(';');
            foreach (var question in questions)
            {
                var i = questions.IndexOf(question);
                var qustAnswerStr = qustAnswerStrList.First(m => m.StartsWith((i + 1) + ":"));
                var answerStr = qustAnswerStr.Split(':')[1];
                if (question.F_Type == 4)
                {
                    answerStr = HttpUtility.UrlDecode(answerStr);
                }
                var answer = new QnAnswerEntity
                {
                    F_PaperId = paperId,
                    F_PaperTitle = paper.F_Title,
                    F_QuestionId = question.F_Id,
                    F_Question = question.F_Question,
                    F_Answer = answerStr,
                    F_CreatorUserId = openId,
                    F_CreatorTime = DateTime.Now
                };
                answer.UpdateOptions(question.F_Type);
                answerList.Add(answer);
            }
            answerApp.AddAnswers(answerList);
            return Success("操作成功。");
        }
    }
}
